package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    DishMapper dishMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);

        ShoppingCart newShoppingCart;
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        if (shoppingCart.getDishId()==null){
            //菜品id为null,因此当前添加的是套餐

            if (list != null && list.size() != 0){
                //已经加过购物车,数量加一
                newShoppingCart = list.get(0);
                newShoppingCart.setNumber(newShoppingCart.getNumber()+1);
                shoppingCartMapper.update(newShoppingCart);

            }else {
                //新的套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());

                shoppingCartMapper.save(shoppingCart);
            }
        }

        if (shoppingCart.getSetmealId() == null){
            //套餐id是空，购物车加的是菜品
            if (list != null && list.size() != 0){
                //购物车里有
                newShoppingCart = list.get(0);
                if (newShoppingCart.getDishFlavor() == shoppingCart.getDishFlavor()){
                    //是同一个商品
                    newShoppingCart.setNumber(newShoppingCart.getNumber()+1);
                    shoppingCartMapper.update(newShoppingCart);
                }else {
                    //规格不同
                    newShoppingCart.setDishFlavor(newShoppingCart.getDishFlavor());
                    shoppingCartMapper.save(newShoppingCart);
                }
            }else {
                //菜品没加过购物车
                DishVO dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());

                shoppingCartMapper.save(shoppingCart);
            }


        }

    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        shoppingCartMapper.delete(shoppingCart);

    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> queryList = shoppingCartMapper.query(shoppingCart);
        if (queryList == null || queryList.size() == 0){
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
        ShoppingCart oldShoppingCart = queryList.get(0);

        oldShoppingCart.setNumber(oldShoppingCart.getNumber()-1);
        if (oldShoppingCart.getNumber() <= 0){
            shoppingCartMapper.delete(shoppingCart);
        }else {
            shoppingCartMapper.update(oldShoppingCart);
        }

    }
}
